package com.gtmonitor.storage;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.View;

import com.gtmonitor.GTmonitorService;
import com.gtmonitor.graphics.GraphicsActivity;

public class StorageView extends View {

    public static int numOfStorages = 10; // To initialize paints and colors
    public static int MAXSIZE = 0;
    public byte[][] storageUsageArray = null;
    public static int index = 0;
    private static int viewWidth = 0;
    private static int viewHeight = 0;
    private static int viewBottom = 0;
    private static int graphHeight = 0;
    private static int graphBottom = 0;
    private static final int textHeight = 15;
    private static Paint[] paints = new Paint[numOfStorages];
    private static int[] reds = new int[numOfStorages];
    private static int[] greens = new int[numOfStorages];
    private static int[] blues = new int [numOfStorages];
    public static boolean[] hide = new boolean [numOfStorages];
    private static Paint paint = new Paint();
    private static final int gray = 100;
    private static final int white = 255;
    private static Paint textPaint = new Paint();
    private static final String percent100 = "100%";
    private static final String percent50 = "50%";
    private static Paint recordPaint = new Paint();
    public static boolean recordFlip = false;

    public StorageView(Context context) {
        super(context);
        int divide = 255 / (numOfStorages + 1);
        MAXSIZE = GTmonitorService.MAXSIZE;
        for (int i=0; i<numOfStorages; ++i) {
            hide[i] = true;
            if (i == GTmonitorService.storageOnBar)
                hide[i] = false;
        }
        for (int i=0; i<numOfStorages; ++i) {
            reds[i] = divide * (i + 1);
            greens[i] = 255;
            blues[i] = (170 + divide * (i + 1)) % 255;
            paints[i] = new Paint();
            paints[i].setColor(Color.argb(100, reds[i], greens[i], blues[i]));
        }
        paint.setColor(Color.argb(50, gray, gray, gray));
        textPaint.setColor(Color.argb(100, white, white, white));
        recordPaint.setColor(Color.RED);
    }
    
    public StorageView(Context context, AttributeSet attr, int defStyle) {
        super(context, attr, defStyle);
        int divide = 255 / (numOfStorages + 1);
        MAXSIZE = GTmonitorService.MAXSIZE;
        for (int i=0; i<numOfStorages; ++i) {
            hide[i] = true;
            if (i == GTmonitorService.storageOnBar)
                hide[i] = false;
        }
        for (int i=0; i<numOfStorages; ++i) {
            reds[i] = divide * (i + 1);
            greens[i] = 255;
            blues[i] = (170 + divide * (i + 1)) % 255;
            paints[i] = new Paint();
            paints[i].setColor(Color.argb(100, reds[i], greens[i], blues[i]));
        }
        paint.setColor(Color.argb(50, gray, gray, gray));
        textPaint.setColor(Color.argb(100, white, white, white));
        recordPaint.setColor(Color.RED);
    }
    
    public StorageView(Context context, AttributeSet attr) {
        super(context, attr); 
        int divide = 255 / (numOfStorages + 1);
        MAXSIZE = GTmonitorService.MAXSIZE;
        for (int i=0; i<numOfStorages; ++i) {
            hide[i] = true;
            if (i == GTmonitorService.storageOnBar)
                hide[i] = false;
        }
        for (int i=0; i<numOfStorages; ++i) {
            reds[i] = divide * (i + 1);
            greens[i] = 255;
            blues[i] = (170 + divide * (i + 1)) % 255;
            paints[i] = new Paint();
            paints[i].setColor(Color.argb(100, reds[i], greens[i], blues[i]));
        }
        paint.setColor(Color.argb(50, gray, gray, gray));
        textPaint.setColor(Color.argb(100, white, white, white));
        recordPaint.setColor(Color.RED);
    }
    
    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
        viewWidth = getWidth();
        viewWidth = viewWidth > MAXSIZE ? MAXSIZE : viewWidth;
        viewHeight = getHeight();
        viewBottom = getBottom();
        viewBottom -= (viewBottom - viewHeight);
        graphHeight = viewHeight - textHeight;
        graphBottom = viewBottom - textHeight;

        numOfStorages = GTmonitorService.numOfStorage;
        if (numOfStorages == 0) return;
        storageUsageArray = GTmonitorService.storageUsageArray;
        index = GTmonitorService.index;
        int indexOnBar = 0;
        
        int i = 0, c, sid = (index - 1 + MAXSIZE) % MAXSIZE;
        for (int s=0; s<numOfStorages; ++s) if (!hide[s]) {
            for (i=sid, c=0; c<viewWidth; ++c) {
                if (storageUsageArray[s][i] > 0) {
                    //to = i - 1; if (to < 0) to = MAXSIZE - 1;
                    //from = i;
                    //canvas.drawLine(c,     viewBottom - (viewHeight * storageUsageArray[s][from] / 100), 
                    //                c + 1, viewBottom - (viewHeight * storageUsageArray[s][to] / 100), paints[s]);
                    canvas.drawLine(c, graphBottom, c, 
                                    graphBottom - (graphHeight * 
                                    storageUsageArray[s][i] / 100), 
                                    paints[s]);
                }
                i = (i - 1 + MAXSIZE) % MAXSIZE;
            }
            indexOnBar = s;
            break;
        }
        
        float zero = graphBottom;
        float twentyfive = graphBottom - graphHeight * 0.25f;
        float fifty = graphBottom - graphHeight * 0.5f;
        float seventyfive = graphBottom - graphHeight * 0.75f;
        float hundred = graphBottom - graphHeight;
        canvas.drawLine(0, zero, viewWidth, zero, paint);
        canvas.drawLine(0, twentyfive, viewWidth, twentyfive, paint);
        canvas.drawLine(0, fifty, viewWidth, fifty, paint);
        canvas.drawLine(0, seventyfive, viewWidth, seventyfive, paint);
        canvas.drawLine(0, hundred, viewWidth, hundred, paint);
        canvas.drawText(percent100, 0, graphBottom - graphHeight + 10, textPaint);
        canvas.drawText(percent50, 0, fifty + 5, textPaint);
        canvas.drawText("0min", 0, viewBottom, textPaint);
        canvas.drawText("1.5", 90, viewBottom, textPaint);
        canvas.drawText("3min", 180, viewBottom, textPaint);
        canvas.drawText("4.5", 270, viewBottom, textPaint);
        canvas.drawText("6min", 360, viewBottom, textPaint);
        canvas.drawText("7.5", 450, viewBottom, textPaint);
        canvas.drawText(GTmonitorService.storageNames[indexOnBar], 0, graphBottom, textPaint);
        
        if (GTmonitorService.storageLogOn && GraphicsActivity.loggable) {
            canvas.drawCircle(viewWidth - 6, hundred + 6, 6, recordPaint);
        }
    }
}
